package com.grape.api;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import com.grape.annotation.OperateMethodName;
import com.grape.constants.Constants;
import com.grape.controller.BaseController;
import com.grape.result.R;
import com.grape.utils.MenuUtils;
import com.grape.vo.BaseVo;
import com.grape.entity.SysMenu;
import com.grape.service.ISysMenuService;
import com.grape.vo.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;


/**
 * 菜单权限表 控制器
 * @author  putao
 * @email   2628279194@qq.com
 */
@RestController
@RequestMapping("menus")
@Api(tags = "菜单权限接口")
@AllArgsConstructor
public class SysMenuController extends BaseController<SysMenu> {

    private final ISysMenuService sysMenuService;

    @GetMapping
    @ApiOperation(value = "菜单权限列表")
    @OperateMethodName(value = "菜单权限表列表", url = "system:menus::listPage")
    public R<?> listPage(BaseVo baseVo) {
        baseVo.setPageSize(10000);
        PageInfo<SysMenu> pageInfo = sysMenuService.pageListByDelFlag(baseVo);
        List<SysMenu> sysMenuList = pageInfo.getList();
        pageInfo.setList(MenuUtils.recursionSysMenuList(Constants.ZERO_STR, sysMenuList).stream().sorted(Comparator.comparingInt(SysMenu::getOrderNum)).collect(Collectors.toList()));
        return CollectionUtil.isNotEmpty(sysMenuList) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }

    @GetMapping("listAllForTree")
    @ApiOperation(value = "菜单权限列表树形")
    @OperateMethodName(value = "菜单权限列表树形", url = "system:menus::listAllForTree")
    public R<?> listAllForTree() {
        List<SysMenu> sysMenuList = sysMenuService.listAllForTree();
        return CollectionUtil.isNotEmpty(sysMenuList) ? selectSuccess(sysMenuList, null) : selectNotFound(sysMenuList, null);
    }

    @PutMapping
    @ApiOperation(value = "菜单权限修改")
    @OperateMethodName(value = "菜单权限表修改", url = "system:menus::update")
    public R<?> update(@Validated @RequestBody SysMenu sysMenu) {
        if (!Strings.isNotBlank(sysMenu.getId())){
            return updateNotFound(sysMenu, null);
        }
        sysMenuService.updateData(sysMenu);
        return updateSuccess(null, null);
    }


    @PostMapping
    @ApiOperation(value = "添加菜单权限")
    @OperateMethodName(value = "添加菜单权限表", url = "system:menus::insert")
    public R<?> insert(@Validated @RequestBody SysMenu sysMenu) {
        return insertSuccess(sysMenuService.insertData(sysMenu), null);
    }

    @DeleteMapping
    @ApiOperation(value = "删除菜单权限，可批量多个id逗号拼接")
    @OperateMethodName(value = "菜单权限表删除,批量", url = "system:menus::delete")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空")  @RequestParam String ids) {
        int number = sysMenuService.deleteData(ids);
        if (number < 0) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }

    @DeleteMapping("deleteData")
    @ApiOperation(value = "删除菜单权限，可批量多个id逗号拼接")
    @OperateMethodName(value = "菜单权限表删除,批量", url = "system:menus::deleteMenu")
    public R<?> deleteMenu(@NotBlank(message = "删除时ids不能为空")  @RequestParam String ids) {
        int number = sysMenuService.deleteMenu(ids);
        if (number < 0) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }

    @GetMapping("detail")
    @ApiOperation(value = "菜单权限详情")
    @OperateMethodName(value = "菜单权限表详情", url = "system:menus::detail")
    public R<?> detail(@NotBlank(message = "详情时id不能为空") String id) {
        SysMenu sysMenu = sysMenuService.detail(id);
        if (sysMenu != null) {
            return selectSuccess(sysMenu, id);
        }
        return selectNotFound(id, null);
    }

    @GetMapping("getMenuByUserId")
    @ApiOperation(value = "查询当前用户对应菜单")
    @OperateMethodName(value = "查询当前用户对应菜单", url = "system:menus::getMenuByUserId")
    public R<?> getMenuByUserId() {
        List<MenuVO> menuVOList = sysMenuService.getMenuByUserId();
        if (CollectionUtil.isNotEmpty(menuVOList)) {
            return selectSuccess(menuVOList.stream().sorted(Comparator.comparingInt(MenuVO::getOrderNum)).collect(Collectors.toList()), null);
        }
        return selectNotFound(null, null);
    }


    @GetMapping("getByRoleId")
    @ApiOperation(value = "根据角色id查询角色菜单和全部菜单")
    @OperateMethodName(value = "根据角色id查询菜单和全部菜单", url = "system:menus::getByRoleId")
    public R<?> getByRoleId(@NotBlank(message = "详情时id不能为空") String roleId) throws ExecutionException, InterruptedException {
        List<MenuVO> parentMenuList = new ArrayList<>();
        List<SysMenu> menuList = sysMenuService.getByRoleId(roleId, parentMenuList);
        Map<String, List<?>> typeKeyAndMenuListValueMap = new HashMap<>();
        typeKeyAndMenuListValueMap.put("sample", menuList);
        typeKeyAndMenuListValueMap.put("tree", MenuUtils.recursionSysMenu(Constants.ZERO_STR, menuList));
        return selectSuccess(typeKeyAndMenuListValueMap, parentMenuList);
    }
}

